package org.projectback.service.ModifyService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.projectback.entity.FileEntity.DTO.FileDTO;
import org.projectback.entity.FileEntity.Format;
import org.projectback.service.ModifyService.ModifyService;
import org.projectback.service.SqlService.FileSqlService;
import org.projectback.utils.Utils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 实现类
 *
 * @author K·K
 * @version 1.0
 * @Date 2025/10/9
 */
@Service
public class ModifyServiceImpl implements ModifyService {
    @Resource
    FileSqlService fileSqlService;
    @Resource
    Utils utils;

    @Override
    public boolean upload(MultipartFile file, long userId, String username) {
        //先获取到用户名,并尝试检查是否创建过该用户文件夹
        String basePath = "C:/Users/huang/Documents/Doc";
        // 1. 原始文件名
        String origName = file.getOriginalFilename();
        if (origName != null) {

            String[] args = utils.splitFileName(origName);

            String baseName = args[0];
            // 2. 后缀
            String ext = args[args.length - 1];
            //创建路径
            Path absolute = Paths.get(basePath, (genDir(username) + origName));
            try {
                //判断类型是否符合要求
                Format format = Format.valueOf(ext);
                // 创建目录
                Files.createDirectories(absolute.getParent());
                // 写盘
                file.transferTo(absolute);   // 简洁且自动关流
                //最后尝试落库
                FileDTO fileDTO = new FileDTO();
                fileDTO.setFormat(format);
                fileDTO.setFileName(baseName);
                fileDTO.setUserId(userId);
                fileDTO.setFilePath(absolute.toString());
                fileDTO.setLastUpdated(getTime());
                return update(fileDTO);
            } catch (IllegalArgumentException | IOException e) {
                return false;
            }
        }
        return false;
    }

    private String getTime() {
        return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
    }

    private String genDir(String username) {
        return "/" + username + "/" + getTime() + "/";
    }

    @Override
    public boolean update(FileDTO fileDTO) {
        //先判断文件是否存在
        LambdaQueryWrapper<FileDTO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FileDTO::getFileName, fileDTO.getFileName());

        FileDTO one = fileSqlService.getOne(lambdaQueryWrapper);
        if (one == null) {
            //System.out.println(fileDTO.getFileName());
            return fileSqlService.save(fileDTO);
        }
        return false;
    }

    @Override
    public boolean delete(String fileName) {
        //先从数据库里将对应数据找出 并封装为对象 接着调用文件删除 将系统文件删除 最后在移除数据库
        return false;
    }
}
